import { Body, Controller, Inject, Post } from '@nestjs/common';
import { UserService } from './user.service';
import { loginDto } from './dto/login.dto';
import { JwtService } from '@nestjs/jwt';
import { UserInfoVo } from './vo/login.vo';

@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) {}

  @Inject(JwtService)
  private jwtService: JwtService;

  @Post('/login')
  async login(@Body() loginDto: loginDto) {
    const res = await this.userService.login(loginDto);

    const vo = new UserInfoVo();
    vo.id = res.id;
    vo.username = res.username;
    vo.avatar = res.avatar;

    if (res) {
      vo.accessToken = this.jwtService.sign({
        userId: res.id,
        username: res.username,
      });
    }
    return vo;
  }
}
